/*
 * @lc app=leetcode.cn id=53 lang=cpp
 *
 * [53] 最大子序和
 */

// @lc code=start
class Solution
{
public:
    int maxSubArray(vector<int> &nums)
    {
        //最大子数组和 贪心
        //主要是寻找局部最优
        int res = INT_MIN;
        int count = 0; //当前计数
        for (int i = 0; i < nums.size(); i++)
        {
            count += nums[i];
            if (count > res)
            {
                res = count;
            }
            //如果加上当前数后变为负数了
            if (count < 0)
            {
                count = 0;
            }
        }
        return res;
    }
};
// @lc code=end

